<template>
  <!--按钮-->
  <div>
    <el-button type="danger" :icon="Plus" @click="SaveBreedingSow">新增</el-button>
    <el-button type="danger" :icon="DocumentAdd">保存</el-button>
    <el-button type="danger" :icon="BottomRight">撤销</el-button>
    <el-button type="danger" :icon="Delete">删除</el-button>
  </div>

  <div style="margin-top: 20px;margin-bottom: 20px;">
    <span>期初日期&nbsp;{{ form[0].openingDate }}</span>
    <span style="margin-left: 100px;">单据号&nbsp;-- --</span>
    <span style="margin-left: 100px;">流水号&nbsp;-- --</span>
  </div>
  <hr size="6" style="background-color:rgb(232.8, 233.4, 234.6)">
  <!--表格-->
  <table>
    <td width="100">序号</td>
    <td width="100">耳号</td>
    <td width="100">成交价(元)</td>
    <td width="100">定价(元)</td>
    <td width="100">折扣比例</td>
    <td width="100">等级</td>
    <td width="100">品种</td>
    <td width="100">性别</td>
    <td width="100">类型</td>
    <td width="100">繁殖状态</td>
    <td width="100">栋舍</td>
    <td width="100">栏位</td>
    <td width="100">出生重(kg)</td>
    <td width="100">出生日期</td>
    <td width="100">断奶重(kg)</td>
    <td width="100">父号</td>
    <td width="100">母号</td>
    <td width="100">月龄</td>


    <tr v-for="(item, index) in form" :key="index">
      <td width="100"><el-input :value="index + 1" /></td>

      <td width="100">
        <el-select v-model="item.basicInforId" placeholder="请选择" clearable>
          <el-option v-for="basic in BasicInformationList.filter((d: any) => d.gender == 2)" :key="basic.basicInforId"
            :label="basic.earNumber" :value="basic.basicInforId">
          </el-option>
        </el-select>
      </td>
      <td width="100"><el-input v-model="item.saleSumMoney" /></td>
      <td width="100"><el-input v-model="item.saleMoney" /></td>
      <td width="100"><el-input v-model="item.saleDiscount" /></td>
      <td width="100"><el-input v-model="item.saleClass" /></td>
      <td width="100"><el-input v-model="item.saleType" /></td>
      <td width="100"><el-input v-model="item.saleSex" /></td>
      <td width="100">
        <el-select v-model="form.pigTypeId" placeholder="请选择">
          <el-option label="长白猪" value="0" />
          <el-option label="杜洛克" value="1" />
          <el-option label="大约克夏猪" value="2" />
          <el-option label="汉普夏猪" value="3" />
          <el-option label="皮特兰猪" value="4" />
          <el-option label="澳湖" value="5" />
        </el-select>
      </td>
      <td width="100"><el-input v-model="item.saleReproduction" /></td>


     <td width="100">
      <el-select v-model="item.bedroomId" placeholder="请选择" @change="handlefield(item.bedroomId, index)" clearable>
          <el-option v-for="bedroom in bedroomList" :key="bedroom.bedroomId" :label="bedroom.bedroomName"
            :value="bedroom.bedroomId" />
        </el-select>
     </td>

    <td width="100">
      <el-select v-model="item.fieldId" placeholder="请选择" clearable>
          <el-option v-for="field in fieldList[index]" :key="field.fieldId" :label="field.fieldName"
            :value="field.fieldId" />
        </el-select>
    </td>



      <td width="100"><el-input v-model="item.saleKg" /></td>
     <td width="120">
      <el-col :span="11">
            <el-date-picker v-model="form.saleTime" type="date" style="width: 200%" />
          </el-col>
     </td>
      <td width="100"><el-input v-model="item.saleDn" /></td>
      <td width="100"><el-input v-model="item.fatherEarNumber" /></td>
      <td width="100"><el-input v-model="item.motherEarNumber" /></td>
      <td width="100"><el-input v-model="item.saleOld" /></td>
    </tr>
  </table>
  <!--增行 删行-->
  <div>
    <el-button type="danger" link @click="addRow">+增行</el-button>
    <el-button type="danger" link @click="removeRow" :disabled="form.length <= 0">-删行</el-button>
  </div>

</template>

<script lang="ts" setup>
import { Plus, Delete, DocumentAdd, BottomRight } from '@element-plus/icons-vue'
import { ref, onMounted, reactive } from 'vue'
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { useRouter, useRoute } from 'vue-router'

const apiUrl = import.meta.env.VITE_APP_API_URL;
const apiBedinUrl = import.meta.env.VITE_APPRead_API_URL;

//创建路由实例
const router = useRouter()
const route = useRoute()

const size = ref<'default' | 'large' | 'small'>('default')

//当前时间
const date = new Date();

//响应式数据 获取
const condition = reactive({
  pageIndex: 1,
  pageSize: 100,
  bedroomTypeId: 0,
  bedroomName: '',
  personName: ''
})

const form = ref<any>([{

  saleId: '',
  basicInforId: '',
  saleSumMoney: '',
  saleMoney: '',
  saleDiscount: '',
  saleClass: '',
  saleType: '',
  saleSex: '',
  pigTypeId: '',
  saleReproduction: '',
  bedroomId: '',
  fieldId: '',
  saleTime: '',
  saleKg: '',
  saleDn: '',
  fatherEarNumber: '',
  motherEarNumber: '',
  saleOld: '',
  isDels: true,
}])
//增行 删行
const addRow = () => {
  form.value.push({
    saleId: '',
  basicInforId: '',
  saleSumMoney: '',
  saleMoney: '',
  saleDiscount: '',
  saleClass: '',
  saleType: '',
  saleSex: '',
  pigTypeId: '',
  saleReproduction: '',
  bedroomId: '',
  fieldId: '',
  saleTime: '',
  saleKg: '',
  saleDn: '',
  fatherEarNumber: '',
  motherEarNumber: '',
  saleOld: '',
  isDels: true,
  })
  console.log(form.value)
}
const removeRow = () => {
  if (form.value.length > 0) {
    form.value.pop()
  }
}
//新增
const SaveBreedingSow = () => {
  console.log(form.value)
  const obj = {
    ablactations: form.value
  }
  axios.post(apiUrl+'/api/Ablactations/addAblactations', obj)
    .then(res => {
      if (res.data.code == 0) {
        ElMessage.success("添加种母期初成功");
        router.push('/ShowAblactation')
      } else {
        ElMessage.error("添加种母期初失败");
      }
    })
}

//猪只信息耳号列表
const BasicInformationList = ref([]);
const GetBasicInformation = () => {
  axios.post('https://localhost:7141/api/Basics/SearchBasic', {})
    .then(res => {
      console.log("猪只信息耳号", res.data.data);
      BasicInformationList.value = res.data.data;
    })
}
//获取栋舍
const bedroomList: any = ref([])
const handleBedroom = () => {
  axios.post(apiUrl+'/api/BedRoom/ShowBedroom', {
    pageIndex: condition.pageIndex,
    pageSize: condition.pageSize,
    bedroomTypeId: condition.bedroomTypeId,
    bedroomName: condition.bedroomName,
    personName: condition.personName
  }).then(res => {
    bedroomList.value = res.data.data.list
  })
}

//获取栏位
//获取栏位
const fieldList: any = ref([])
const handlefield = (id: number, index: number) => {
  form.value[index].fieldId = ''
  axios.post(apiUrl+'/api/BedRoom/GetBrdroomIdField', {
    bedroomId: id
  }).then(res => {
    fieldList.value[index] = res.data.data;
    console.log("栏位", fieldList.value[index], index);
  })
}

//创建生命周期函数钩
onMounted(() => {

  handleBedroom();
  GetBasicInformation();
})
</script>
